/// <reference name="MicrosoftAjax.js" />
/// <reference name="dnn.js" assembly="DotNetNuke.WebUtility" />
/// <reference name="Silverlight.js" />

Type.registerNamespace('$YourCompany$');
    
$YourCompany$.View$safeprojectname$ = function()
{
    //Call Base Method
    $YourCompany$.View$safeprojectname$.initializeBase(this);
    //Member Variables
    this._controls = [];
    this._msgs = {};
    this._xapPath = null;
    this._slCtl = null; //silverlight control reference
    this._width = 300;
    this._height = 150;

    //Associated delegates to single member variable dictionary to make it easy to dispose
    this._delegates = {
        _onLoadDelegate: Function.createDelegate(this, this._onLoad),
        _onSLLoadDelegate: Function.createDelegate(this, this._onSLLoad),
        _onNameObtainedDelegate: Function.createDelegate(this, this._onNameObtained)
    };

    //Event Hookup
    Sys.Application.add_load(this._delegates._onLoadDelegate);
}

$YourCompany$.View$safeprojectname$.prototype =
{
    //Properties
    get_ns: function() { return this.get_id() + '_'; },
    get_msgs: function() { return this._msgs; },
    set_msgs: function(value) { this._msgs = value; },
    get_settings: function() { return this._settings; },
    set_settings: function(value) { this._settings = value; },
    get_xapPath: function() { return this._xapPath; },
    set_xapPath: function(value) { this._xapPath = value; },
    get_name: function()
    {
        return this._getControl('txtName').value;
    },
    set_name: function(val)
    {
        this._getControl('txtName').value = val;
    },

    //Events
    initialize: function()
    {
        //Call Base Method    
        $YourCompany$.View$safeprojectname$.callBaseMethod(this, 'initialize');

        //hookup event handlers
        var slid = this.get_ns() + '_sl';   //generate unique silverlight id
        var ctl = this._getControl('divSL'); //obtain ref to silverlight container
        Silverlight.createObject(this.get_xapPath(), ctl, slid,
            { width: this._width, height: this._height, background: '#00FFFFFF', windowless: 'true', version: "2.0.31005.0" },
            { onLoad: this._delegates._onSLLoadDelegate });

        if (this._settings['Color'])
            ctl.style.backgroundColor = this._settings['Color'];
        
        $addHandlers(this._getControl('txtName'), { "change": this._onNameChange }, this);

    },

    _onLoad: function(src, arg)
    {

    },

    _onSLLoad: function(sender, args)
    {
        //on silverlight load event initialize its properties
        this._slCtl = sender;
        sender.Content.PageJS.CallbackID = String.format("$YourCompany$.Modules.$safeprojectname$.View$safeprojectname$.{0}", this.get_id());    //assign callback module identifier
        sender.Content.PageJS.UserName = this.get_name();
        sender.Content.PageJS.RealNameObtained = this._delegates._onNameObtainedDelegate;   //allow javascript to handle event that silverlight raises
    },

    _onNameObtained: function(src, arg)
    {
        this.set_name(arg.RealName);   //assign real name if logged in
        this._getControl('txtName').disabled = true;
    },

    _onNameChange: function(src, arg)
    {
        this._slCtl.Content.PageJS.UserName = this.get_name();
    },

    //Methods
    
    //Private Methods
    _getControl: function(id)
    {
        if (this._controls[id] == null)
        {
            this._controls[id] = $get(id);
            if (this._controls[id] == null)
                this._controls[id] = $get(this.get_ns() + id);
        }
        return this._controls[id];
    },
    
    dispose: function()
    {
        for (var id in this._controls)
        {
            if (this._controls[id].tagName != null)
                $clearHandlers(this._controls[id]);
        }
        this._delegates = null;
        $YourCompany$.View$safeprojectname$.callBaseMethod(this, 'dispose');
    }
}

//register class and inherit from Sys.Component
$YourCompany$.View$safeprojectname$.registerClass('$YourCompany$.View$safeprojectname$', Sys.Component);
